<template>
<j-modal :title="title" :width="width" :visible="visible" :confirmLoading="confirmLoading" switchFullscreen @ok="handleOk" @cancel="handleCancel" cancelText="关闭">
    <a-spin :spinning="confirmLoading">
        <a-form :form="form">
            <a-row>
                <a-col :span="12">
                    <a-form-item v-if="title=='新增'" :required="true" label="收货号" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <j-dict-select-request ref="dropData" url="/procurement/purchaseReceiptSheet/listWTH" v-model="dataList.receiptSheetOdd" type="list" :trigger-change="true" @change="getData" />
                    </a-form-item>
                    <a-form-item v-else label="收货号" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-model="dataList.receiptSheetOdd" placeholder="请输入收货号"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="退货号" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-model="dataList.odd" placeholder="请输入退货号"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <j-date placeholder="请选择日期" v-decorator="['date']" :trigger-change="true" style="width: 100%" />
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="供应商" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-model="dataList.supplier" placeholder="请输入供应商"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="仓库" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-model="dataList.warehouse" placeholder="请输入仓库"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="发票类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input v-decorator="['invoiceType']" placeholder="请输入发票类型"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="退款类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input v-decorator="['refundType']" placeholder="请输入退款类型"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="收款账户" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input v-decorator="['paymentAccount']" placeholder="请输入收款账户"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="收款方式" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input v-decorator="['paymentMethods']" placeholder="请输入收款方式"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="联系人" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-model="dataList.contact" placeholder="请输入联系人"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="联系电话" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-model="dataList.phone" placeholder="请输入联系电话"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="出票金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input v-decorator="['ticketAmount']" placeholder="请输入出票金额"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="票号" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input v-decorator="['banks']" placeholder="请输入票号"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="凭证号" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input v-decorator="['voucherNumber']" placeholder="请输入凭证号"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="业务员" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-model="dataList.salesman" placeholder="请输入业务员"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="摘要" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-model="dataList.digest" placeholder="请输入摘要"></a-input>
                    </a-form-item>
                </a-col>
            </a-row>
        </a-form>
    </a-spin>
</j-modal>
</template>

<script>
import {
    httpAction,
    getAction
} from '@/api/manage'
import pick from 'lodash.pick'
import JDate from '@/components/jeecg/JDate'

export default {
    name: "PurchaseReturnModal",
    components: {
        JDate,
    },
    props: ['odd'],
    data() {
        return {
            form: this.$form.createForm(this),
            title: "操作",
            width: 800,
            visible: false,
            model: {},
            dataList: {},
            labelCol: {
                sm: {
                    span: 6
                },
            },
            wrapperCol: {
                sm: {
                    span: 16
                },
            },

            confirmLoading: false,
            validatorRules: {},
            url: {
                add: "/procurement/purchaseReturn/add",
                edit: "/procurement/purchaseReturn/edit",
            }

        }
    },
    watch: {
        odd() {
            if (this.odd) {
                this.$set(this.dataList, 'odd', this.odd)
            }
        }
    },
    methods: {
        async getData(value) {
            this.$set(this.dataList, 'receiptSheetOdd', value);
            let url = "/procurement/purchaseReceiptSheet/list";
            let params = {
                odd: value,
            }
            let result = await getAction(url, params);
            let {
                dataList
            } = this;
            let {
                supplier,
                supplierId,
                contact,
                contactPhone,
                warehouse,
                warehouseId,
                salesman,
                digest
            } = result.result.records[0]
            this.$set(dataList, 'supplier', supplier)
            this.$set(dataList, 'supplierId', supplierId)
            this.$set(dataList, 'contact', contact)
            this.$set(dataList, 'phone', contactPhone);
            this.$set(dataList, 'warehouse', warehouse)
            this.$set(dataList, 'warehouseId', warehouseId)
            this.$set(dataList, 'salesman', salesman)
            this.$set(dataList, 'digest', digest)
        },
        add() {
            this.edit({});
        },
        edit(record) {
            this.form.resetFields();
            let {
                supplier,
                contact,
                phone,
                warehouse,
                salesman,
                digest,
                receiptSheetOdd
            } = record;
            this.dataList = {
                supplier,
                contact,
                phone,
                warehouse,
                salesman,
                digest,
                receiptSheetOdd,
            }
            this.odd && (!record.receiptSheetOdd) ? this.$set(this.dataList, 'odd', this.odd) : this.$set(this.dataList, 'odd', record.odd)
            this.model = Object.assign({}, record);
            this.visible = true;
            this.$nextTick(() => {
                this.form.setFieldsValue(pick(this.model, 'date', 'invoiceType', 'refundType', 'paymentAccount', 'paymentMethods', 'ticketAmount', 'banks', 'voucherNumber'))
            })
        },
        close() {
            this.$emit('close');
            this.visible = false;
        },
        handleOk() {
            const that = this;
            // 触发表单验证
            if (this.dataList.receiptSheetOdd == '' || this.dataList.receiptSheetOdd == undefined) {
                this.$message.warning('请选择收货号!')
                return false;
            }
            this.form.validateFields((err, values) => {
                if (!err) {
                    that.confirmLoading = true;
                    let httpurl = '';
                    let method = '';
                    if (!this.model.id) {
                        httpurl += this.url.add;
                        method = 'post';
                    } else {
                        httpurl += this.url.edit;
                        method = 'put';
                    }
                    values = Object.assign(values, this.dataList);
                    let formData = Object.assign(this.model, values);
                    httpAction(httpurl, formData, method).then((res) => {
                        if (res.success) {
                            that.$message.success(res.message);
                            that.$emit('ok');
                        } else {
                            that.$message.warning(res.message);
                        }
                    }).finally(() => {
                        that.confirmLoading = false;
                        that.close();
                        that.$refs.dropData.sendData();
                    })
                }

            })
        },
        handleCancel() {
            this.close()
        },
        popupCallback(row) {
            this.form.setFieldsValue(pick(row, 'date', 'invoiceType', 'refundType', 'paymentAccount', 'paymentMethods', 'ticketAmount', 'banks', 'voucherNumber'))
        },

    }
}
</script>
